﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using enqueteJwVd;
using enqueteJwVd.DAL;
using enqueteJwVd.Services;
using enqueteJwVd.ViewModels;
using System.Collections;

namespace enqueteJwVd.Controllers
{
    [Authorize(Roles = "e-manager")]
    public class UserSurveysController : Controller
    {
        UnitOfWork unitOfWork = new UnitOfWork();
        IUserSurveyService userSurveyService;

        public UserSurveysController(IUserSurveyService userSurveyService)
        {
            this.userSurveyService = userSurveyService;
        }

        public UserSurveysController()
        {

        }

        //
        // GET: /UsrSurvey/

        public ViewResult Index()
        {
            var userSurveys = userSurveyService.GetAllUserSurveys();
            var viewModel = new UserSurveyIndexViewModel() { UserSurveys = userSurveys };
            return View(viewModel);
        }

        //
        // GET: /UsrSurvey/Details/5

        public ViewResult Details(int id)
        {
            UserSurvey usersurvey = unitOfWork.UserSurveyRepository.GetById(id);
            return View(usersurvey);
        }

        //
        // GET: /UsrSurvey/Create

        public ActionResult Link()
        {
            ViewBag.SurveyFkId = new SelectList(unitOfWork.SurveyRepository.Get(), "Id", "Title");
            
            ViewBag.usersList = GetUsers(null);
            ViewBag.UserFkId = new SelectList(unitOfWork.UserRepository.Get(), "Id", "VoorAchterNaam");
            return View();
        }

       private MultiSelectList GetUsers(string[] selectedValues)
        {
           
            List<User> usersList = new List<User>();
            usersList.Add(null);

            IEnumerable<User> users = unitOfWork.UserRepository.GetAll();
            foreach (User user in users)
            {
                usersList.Add(user);
            }
                        return new MultiSelectList(usersList, "Id", "VoorAchternaam", selectedValues);
        }

        //
        // POST: /UsrSurvey/Create
        [HttpPost]
       public ActionResult Link([Bind(Exclude = "Id")] UserSurvey userSurvey)
        //public ActionResult Link([Bind(Exclude = "Id")] UserSurvey userSurvey, FormCollection form)
        /*
         * The FormCollection returns all the input's that were submitted 
         * via the html form and you are able to retrieve those elements 
         * by using the following syntax formCollection["Title"] given 
         * that the input element's name is "Title"
         * */
        {    
            if (ModelState.IsValid)
            {
                if (this.CheckList(userSurvey).Count() == 0)
                {//checken of combinatie al bestaat!!
                    unitOfWork.UserSurveyRepository.Add(userSurvey);
                    unitOfWork.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    //http://stackoverflow.com/questions/5739362/modelstate-addmodelerror-how-can-i-add-an-error-that-isnt-for-a-property
                    ModelState.AddModelError(string.Empty, "This user - survey combination already exists");
                }
              
            }

            ViewBag.SurveyFkId = new SelectList(unitOfWork.SurveyRepository.Get(), "Id", "Title", userSurvey.SurveyFkId);
            ViewBag.UserFkId= new SelectList( unitOfWork.UserRepository.Get(), "Id", "VoorAchternaam", userSurvey.UserFkId);

            //ViewBag.YouSelected = form["usersList"];
            //string selectedValues = form["usersList"];
            //ViewBag.userslist = GetUsers(selectedValues.Split(','));  

            return View(userSurvey);
        }

        //
        // GET: /UsrSurvey/Edit/5

        public ActionResult Edit(int? id)
        {
            UserSurvey userSurvey = unitOfWork.UserSurveyRepository.GetById(id);
            ViewBag.SurveyFkId = new SelectList(unitOfWork.SurveyRepository.Get(), "Id", "Title");
            ViewBag.Userslist = GetUsers(null);
            return View(userSurvey);
        }

        private List<UserSurvey> CheckList(UserSurvey userSurvey)
        {
            List<UserSurvey> checkList = new List<UserSurvey>();
            IQueryable<UserSurvey> list = unitOfWork.UserSurveyRepository.GetAll();
            foreach (UserSurvey us in list)
            {
                if (us.UserFkId == userSurvey.UserFkId && us.SurveyFkId == userSurvey.SurveyFkId)
                {
                    checkList.Add(us);// zou normaal max. 1 member mogen bevatten

                }
            }
                    return checkList;
        }

        //
        // POST: /UsrSurvey/Edit/5

        [HttpPost]
         public ActionResult Edit(UserSurvey userSurvey)
            //public ActionResult Edit(UserSurvey userSurvey, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                if (this.CheckList(userSurvey).Count() == 0)
                {//checken of combinatie al bestaat!!
                    unitOfWork.UserSurveyRepository.Update(userSurvey);
                    unitOfWork.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    //http://stackoverflow.com/questions/5739362/modelstate-addmodelerror-how-can-i-add-an-error-that-isnt-for-a-property
                    ModelState.AddModelError(string.Empty, "This user - survey combination already exists");
                }
            }
            ViewBag.SurveyFkId = new SelectList(unitOfWork.SurveyRepository.Get(), "Id", "Title", userSurvey.SurveyFkId);
            ViewBag.UserFkId = new SelectList(unitOfWork.UserRepository.Get(), "Id", "VoorAchternaam", userSurvey.UserFkId);
           // ViewBag.YouSelected = form["UsersList"];
            //string selectedValues = form["UsersList"];
            //ViewBag.Userslist = GetUsers(selectedValues.Split(','));
            return View(userSurvey);
        }

        //
        // GET: /UsrSurvey/Delete/5

        public ActionResult Delete(int id)
        {
            UserSurvey userSurvey = unitOfWork.UserSurveyRepository.GetById(id);
            return View(userSurvey);
        }

        //
        // POST: /UsrSurvey/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            UserSurvey userSurvey = unitOfWork.UserSurveyRepository.GetById(id);
            unitOfWork.UserSurveyRepository.Delete(userSurvey);
            unitOfWork.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            unitOfWork.Dispose();
            base.Dispose(disposing);
        }

        
    }
}